// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: MIT

import { Elevation } from "./elevation.slint";
import { BaseButton } from "./base_button.slint";
import { MaterialTypography } from "../styling/material_typography.slint";
import { MaterialPalette } from "../styling/material_palette.slint";
import { MaterialStyleMetrics } from "../styling/material_style_metrics.slint";

export component TonalIconButton {
    in property <image> icon;
    in property <image> checked_icon: root.icon;
    in property <bool> checkable;
    in_out property <bool> checked;
    in property <string> tooltip <=> base.tooltip;
    in property <bool> enabled <=> base.enabled;

    callback clicked();

    accessible-role: button;
    accessible-enabled: root.enabled;
    accessible-label: root.tooltip;
    accessible-checkable: root.checkable;
    accessible-checked: root.checked;
    accessible-action-default => { base.clicked(); }

    background_layer := Rectangle {
        width: 100%;
        height: 100%;
        border_radius: base.border_radius;
        background: root.checkable ? MaterialPalette.surface_container_highest : MaterialPalette.secondary_container;
        visible: root.enabled;
    }

    base := BaseButton {
        icon: root.checked ? root.checked_icon : root.icon;
        border_radius: self.height / 2;
        color: !root.enabled ? MaterialPalette.on_surface_variant : MaterialPalette.on_secondary_container;
        button_horizontal_padding: 0;
        button_vertical_padding: 0;
        width: self.height;
        icon_size: MaterialStyleMetrics.icon_size_24;

        clicked => {
            root.toggle();
            root.clicked();
        }
    }

    function toggle() {
        if !root.checkable {
            return;
        }

        root.checked = !root.checked;
    }

    states [
        checked when root.enabled && root.checked : {
            background_layer.background: MaterialPalette.secondary_container;
        }
    ]
}
